<template>
	<view>
		<uv-status-bar />
		<g-tabs :tabs="tabs" :active="active" status-bar @change="changeTab" />
		<Arrange v-if="active === 'arrange'" :refresh="refresh" :onMore="onMore"
			@onRefresh="(v) => emit('update:refresh', v)" @onReach="(v) => emit('update:onMore', v)" />
		<Schedule v-if="active === 'schedule'" :leader="leader" :refresh="refresh" :onMore="onMore"
			@onRefresh="(v) => emit('update:refresh', v)" @onReach="(v) => emit('update:onMore', v)" />
		<Data-board v-else-if="active === 'board'" :refresh="refresh" :onMore="onMore"
			@onRefresh="(v) => emit('update:refresh', v)" @onReach="(v) => emit('update:onMore', v)" />
		<Untreated v-else-if="active === 'untreated'" :refresh="refresh" :approver="approver" @count="handleCount"
			@onRefresh="(v) => emit('update:refresh', v)" />
		<Attendance v-else-if="active === 'attendance'" :refresh="refresh" :leader="leader" :onMore="onMore"
			@onRefresh="(v) => emit('update:refresh', v)" @onReach="(v) => emit('update:onMore', v)" />
	</view>
</template>
<script setup>
	import {
		ref
	} from "vue";
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		getStatistics
	} from "@/request/home";
	import Arrange from "@/pages/Home/cp/Arrange.vue";
	import Schedule from "@/pages/Home/cp/Schedule.vue";
	import DataBoard from "@/pages/Home/cp/DataBoard.vue";
	import Untreated from "@/pages/Home/cp/Untreated.vue";
	import Attendance from "@/pages/Home/cp/Attendance.vue";

	const props = defineProps({
		// 是否下拉刷新
		refresh: {
			type: Boolean,
			default: false,
		},
		// 上拉加载
		onMore: {
			type: Boolean,
			default: false,
		},
	});

	const emit = defineEmits(["update:refresh", "update:onMore", "count"]);

	const active = ref(); // tab下标
	const leader = ref(""); // 项目负责人
	const approver = ref(""); // 审批人
	const tabs = ref([]); // tab列表

	onLoad((e) => {
		setTimeout(() => {
			const user = uni.getStorageSync("userInfo");
			if (user) {
				leader.value = user.projectLeader;
				approver.value = user.approver;
			}
			getColumn();
			getUnTaskCount();
			if ("type" in e) {
				active.value = e.type;
			} else {
				active.value = tabs.value[0].type;
			}
		}, 0);
	});

	// 获取tab列表
	const getColumn = () => {
		let list = [];
		if (leader.value == "1") {
			list.push({
				type: "arrange",
				label: "日程安排",
				badge: {
					value: 0
				}
			});
		}
		list.push({
			type: "schedule",
			label: "我的日程",
			badge: {
				value: 0
			}
		});
		list.push({
			type: "board",
			label: "数据看板",
			badge: {
				value: 0
			}
		});
		list.push({
			type: "untreated",
			label: "待办事项",
			badge: {
				value: 0
			}
		});
		list.push({
			type: "attendance",
			label: "打卡动态",
			badge: {
				value: 0
			}
		});
		tabs.value = list;
	};

	// tab切换
	const changeTab = (val) => {
		active.value = val.type;
		uni.stopPullDownRefresh();
	};

	// 统计数量
	const handleCount = (val) => {
		const index = tabs.value.findIndex((i) => i.label == val.label);
		if (index != -1) {
			tabs.value[index].badge.value = val.count;
		}
	};

	// 获取待办任务数量
	const getUnTaskCount = () => {
		getStatistics().then((res) => {
			if (res.code == 200) {
				const {
					taskNum,
					reportNum,
					handleWorkOrderNum
				} = res.data;
				let count = taskNum + handleWorkOrderNum;
				if (approver.value == "1") count += reportNum;
				tabs.value[leader.value == "1" ? 3 : 2].badge.value = count;
			}
		});
	};
</script>
<style lang="scss" scoped></style>